﻿using Mengsk.ProxyDelivery.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Mengsk.ProxyDelivery.App.UI.Operator
{
    /// <summary>
    /// PasswordResetWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ResetPasswordWindow : Window
    {
        public Mengsk.ProxyDelivery.Domain.Operator Operator { get; set; }
        private OperatorService operatorService = ServiceContainer.GetService<OperatorService>();

        public ResetPasswordWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.Title = "重置密码:" + this.Operator.Number;
        }

        private void btnSendSms_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.operatorService.SendResetPwdSms(this.Operator.Id, this.Operator.Number);
                this.tbSmsMessage.Text = "已发送:" + this.Operator.Phone.Substring(0, 3) + "****" + this.Operator.Phone.Substring(7, 4) + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            string newPwd = this.pbNewPassword.Password;
            string newPwd2 = this.pbRepeatNewPassword.Password;
            string smsCode = this.tbSmsCode.Text.Trim();

            if (newPwd != newPwd2)
            {
                MessageBox.Show("两次密码不一致");
                return;
            }

            if (string.IsNullOrWhiteSpace(smsCode))
            {
                MessageBox.Show("短信验证为空");
                return;
            }

            try
            {
                this.operatorService.UpdatePassword(this.Operator.Id, this.Operator.Number, this.Operator.Name, Utls.MD5Util.Md5(newPwd), smsCode);
                MessageBox.Show("已更新用户密码");
                this.DialogResult = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
